package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.CheckItem;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.checkitemService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 体检检查项管理  服务消费者
 */
///checkitem/add.do
//checkitem/findpage.do
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private checkitemService checkitemService;

    //因为添加检查项的表单传过来的是json字符串
    //我们需要用@RequestBody转换为原来的样子
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    public Result addCheckitem(@RequestBody CheckItem checkItem) {
        Result result = checkitemService.addCheckitem(checkItem);
        return result;
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findpage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        /*
        * private Integer currentPage;//页码
          private Integer pageSize;//每页记录数
          private String queryString;//查询条件
          */
        PageResult pageResult = checkitemService.findPage(queryPageBean);
        /*
        *  private Long total;//总记录数
           private List rows;//当前页结果
        * */
        return pageResult;
    }

    /**
     * 删除检查项
     * /checkitem/delete.do
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result deleteCheckitem(@PathVariable(value = "id") Integer id) {
        //前台传过来的是json类型的字符串,要用@RequestBody 将其转为原来的样子
        System.out.println("Controller::" + id);
        /*
         留出问题,为什么要用Interger,数据库中用的是int
         而且这个传过来的是 id必有的 不可能为空不存在null的情况.
        *
        * */
        /*
         用文档中老师的写法,不用service传过来的返回值
         用try catch 去抓异常 从而判断是否删除成功
         */
        try {
            checkitemService.deleteCheckitem(id);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }

        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        /*
        *
        * private boolean flag;//执行结果，true为执行成功 false为执行失败
            private String message;//返回结果信息，主要用于页面提示信息
           private Object data;//返回数据
        * */
    }

    /**
     * 根据id查找Checkitem
     * @param id
     * @return
     */
    //"/checkitem/findCheckitemByid?id="+row.id
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findCheckitemByid")
    public Result findCheckitemById(Integer id) {
      Result result   =  checkitemService.findCheckitemByid(id);
        return result;
    }

    ///checkitem/edit.do
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    public Result updateCheckitem(@RequestBody  CheckItem checkItem){
        //前端传过来的是json字符串 需要用@RequestBody注解
        System.out.println(checkItem);
        try {
            checkitemService.updataCheckitem(checkItem);
            return  new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误,请联系15385692072");
        }
    }
}
